package utils

import (
	"bytes"
	"os"
	"path/filepath"
	"runtime"
	"strconv"
	"strings"
)

// GetExecPath / 获取本程序二进制文件路径
func GetExecPath() (string, error) {
	dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
	if err != nil {
		return "", err
	}
	return dir, err
}

// GetExecName / 获取可执行文件名称
func GetExecName() string {
	exeP := os.Args[0]
	exeP = strings.Replace(exeP, "\\", "/", -1)
	idx := strings.LastIndex(exeP, "/")
	exeName := exeP[idx+1:]
	return exeName
}

// GetGID / 获取goroutine的ID
func GetGID() uint64 {
	b := make([]byte, 64)
	b = b[:runtime.Stack(b, false)]
	b = bytes.TrimPrefix(b, []byte("goroutine "))
	b = b[:bytes.IndexByte(b, ' ')]
	n, _ := strconv.ParseUint(string(b), 10, 64)
	return n
}
